<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Dcat\Admin\Layout\Content;

class PickupCommissionController extends Controller
{
    /**
     * 提成明细
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->title('提成明细')
            ->description('查看自提点提成统计')
            ->body($this->renderCommissionList());
    }

    /**
     * 提成详情
     *
     * @param Content $content
     * @param int $id
     * @return Content
     */
    public function show(Content $content, $id)
    {
        return $content
            ->title('提成详情')
            ->description('查看详细提成信息')
            ->body($this->renderCommissionDetail($id));
    }

    /**
     * 提成结算
     *
     * @param Content $content
     * @return Content
     */
    public function settlement(Content $content)
    {
        return $content
            ->title('提成结算')
            ->description('处理提成结算')
            ->body($this->renderCommissionSettlement());
    }

    /**
     * 渲染提成列表
     *
     * @return string
     */
    protected function renderCommissionList()
    {
        $commissions = [
            [
                'id' => 1,
                'pickup_point' => '北京朝阳自提点',
                'month' => '2024-01',
                'total_orders' => 856,
                'commission_rate' => '2.5%',
                'total_amount' => 12580.00,
                'commission_amount' => 314.50,
                'status' => '已结算',
                'settled_at' => '2024-02-05'
            ],
            [
                'id' => 2,
                'pickup_point' => '上海浦东自提点',
                'month' => '2024-01',
                'total_orders' => 1203,
                'commission_rate' => '3.0%',
                'total_amount' => 18450.00,
                'commission_amount' => 553.50,
                'status' => '已结算',
                'settled_at' => '2024-02-05'
            ],
            [
                'id' => 3,
                'pickup_point' => '广州天河自提点',
                'month' => '2024-02',
                'total_orders' => 672,
                'commission_rate' => '2.5%',
                'total_amount' => 9860.00,
                'commission_amount' => 246.50,
                'status' => '待结算',
                'settled_at' => null
            ],
            [
                'id' => 4,
                'pickup_point' => '深圳南山自提点',
                'month' => '2024-02',
                'total_orders' => 945,
                'commission_rate' => '3.0%',
                'total_amount' => 14320.00,
                'commission_amount' => 429.60,
                'status' => '待结算',
                'settled_at' => null
            ]
        ];

        $statistics = [
            'total_commission_this_month' => 676.10,
            'pending_settlement' => 676.10,
            'settled_this_month' => 868.00,
            'total_pickup_points' => 4
        ];

        return view('admin.pickup.commissions', compact('commissions', 'statistics'));
    }

    /**
     * 渲染提成详情
     *
     * @param int $id
     * @return string
     */
    protected function renderCommissionDetail($id)
    {
        $commission = [
            'id' => $id,
            'pickup_point' => '北京朝阳自提点',
            'contact' => '张三',
            'phone' => '13800138001',
            'month' => '2024-01',
            'commission_rate' => 2.5,
            'total_orders' => 856,
            'total_amount' => 12580.00,
            'commission_amount' => 314.50,
            'status' => '已结算',
            'settled_at' => '2024-02-05 15:30:00',
            'settlement_note' => '按时结算，无异议'
        ];

        $orderDetails = [
            ['date' => '2024-01-01', 'orders' => 28, 'amount' => 420.00, 'commission' => 10.50],
            ['date' => '2024-01-02', 'orders' => 32, 'amount' => 480.00, 'commission' => 12.00],
            ['date' => '2024-01-03', 'orders' => 25, 'amount' => 375.00, 'commission' => 9.38],
            ['date' => '2024-01-04', 'orders' => 35, 'amount' => 525.00, 'commission' => 13.13],
            ['date' => '2024-01-05', 'orders' => 42, 'amount' => 630.00, 'commission' => 15.75]
        ];

        return view('admin.pickup.commission_detail', compact('commission', 'orderDetails'));
    }

    /**
     * 渲染提成结算
     *
     * @return string
     */
    protected function renderCommissionSettlement()
    {
        $pendingSettlements = [
            [
                'id' => 3,
                'pickup_point' => '广州天河自提点',
                'month' => '2024-02',
                'commission_amount' => 246.50,
                'selected' => false
            ],
            [
                'id' => 4,
                'pickup_point' => '深圳南山自提点',
                'month' => '2024-02',
                'commission_amount' => 429.60,
                'selected' => false
            ]
        ];

        return view('admin.pickup.settlement', compact('pendingSettlements'));
    }
}